package com.allen.mapper;

import com.allen.bean.Like;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * @Author: allen
 * @Data: 2021.06.23.16:50
 **/

@Mapper //标记mapper文件位置，否则在Application.class启动类上配置mapper包扫描
@Repository
public interface LikeMapper {

    /**
     * 查询有没有给对应作品点赞
     * 1.没有查询到----没有点赞
     * 2.查询到，但已点赞
     */


    /**
     *查询id是否存在
     * @param like
     * @return
     */
    @Select(value="select id from u_like where w_username=#{w_username} and w_font=#{w_font} and c_username=#{c_username}")
    @Results
            ({@Result(property = "w_username",column = "w_username"),
                    @Result(property = "w_font",column = "w_font"),
                    @Result(property = "c_username",column = "c_username"),
                    @Result(property = "u_like",column = "u_like")})    //@Result：为了解决数据库字段名与实体类属性名不一致的问题,//column:数据库字段名，property：实体类属性名
    Like findIdByName(String w_username,String w_font,String c_username);

    /**
     * 查询是否点赞
     * @param like
     * @return
     */
    @Select(value="select u_like from u_like where w_username=#{w_username} and w_font=#{w_font} and c_username=#{c_username}")
//    @Results
//            ({@Result(property = "w_username",column = "w_username"),
//                    @Result(propercty = "w_font",column = "w_font"),
//                    @Result(property = "c_username",column = "c_username"),
//                    @Result(property = "u_like",column = "u_like")})    //@Result：为了解决数据库字段名与实体类属性名不一致的问题,//column:数据库字段名，property：实体类属性名
    Boolean findLikeByName(Like like);

    /**
     * 更改是否点赞的值(改为已经点赞)
     */
    @Update(value="update u_like set u_like=true where w_username=#{w_username} and w_font=#{w_font} and c_username=#{c_username}")
    Integer updataLiket(Like like);

    /**
     * 添加点赞信息
     */
    @Insert(value="insert into u_like values(#{id},#{w_username},#{w_font},#{c_username},#{u_like})")
    Integer addLike(Like like);

    /**
     * 点赞成功时数据库中的点赞总数+1
     */
    @Update(value="update myworks set sum_like=sum_like+1 where username=#{w_username} and u_font=#{w_font}")
    void appendlike(Like like);
}
